import django_filters
from .models import Server, SrvAccount

class ServerStatusFilter(django_filters.FilterSet):

    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    astag = django_filters.CharFilter(field_name='astag', lookup_expr='icontains')
    hostname = django_filters.CharFilter(field_name='hostname', lookup_expr='icontains')
    ipaddress = django_filters.CharFilter(field_name='ipaddress', lookup_expr='icontains')
    os_version = django_filters.CharFilter(field_name='os_version', lookup_expr='icontains')
    sub_ipaddress = django_filters.CharFilter(field_name='sub_ipaddress', lookup_expr='icontains')
    owner_func = django_filters.CharFilter(field_name='owner_func', lookup_expr='icontains')
    owner = django_filters.CharFilter(field_name='owner', lookup_expr='icontains')
    in_use = django_filters.BooleanFilter(method='filter_by_in_use')

    class Meta:
        model = Server
        fields = []

    def filter_by_in_use(self, queryset, name, value):
        if value:
            return queryset.filter(form_offline__isnull=True)
        return queryset.filter(form_offline__isnull=False)
    
class SrvAccountFilter(django_filters.FilterSet):

    server = django_filters.CharFilter(field_name='server__id', lookup_expr='exact')
    server_ip = django_filters.CharFilter(field_name='server__ipaddress', lookup_expr='icontains')
    account = django_filters.CharFilter(field_name='account', lookup_expr='icontains')
    is_change = django_filters.BooleanFilter(field_name='is_change', lookup_expr='exact')

    class Meta:
        model = SrvAccount
        fields = []
    
